package com.bot.adms.dao.impl;

import com.bot.adms.dao.IApplyContentSpScDetailPosterDao;
import com.bot.adms.model.entity.ApplyContentSpScDetailPoster;
import com.bot.adms.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/*
 * 投放申请海报信息持久层接口实现
 * @author 刘孜涵
 * */
public class ApplyContentSpScDetailPosterDaoImpl implements IApplyContentSpScDetailPosterDao {
    private Connection conn;
    private Statement stmt;
    private ResultSet rs;

    /*
      添加投放申请海报信息
      @param applyContentSpScDetailPoster 投放申请海报信息 包含：投放申请海报标识，海报文件，分辨率等
     * @return Integer 添加成功数量
     */
    @Override
    public Integer insertApplyContentSpScDetailPoster(ApplyContentSpScDetailPoster applyContentSpScDetailPoster) {
        try {
            //创建连接
            conn = JdbcUtils.getConnection();
            //根据连接获取statemen对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "INSERT INTO `bo_adms`.`apply_content_sp_sc_detail_poster` ( `poster_url`, `resolution`, `apply_content_sp_sc_detail_id`) " +
                    "VALUES ('"+applyContentSpScDetailPoster.getPosterUrl()+"'," +
                    " "+applyContentSpScDetailPoster.getResolution()+", " +
                    ""+applyContentSpScDetailPoster.getApplyContentSpScDetailId()+");";
            int count = stmt.executeUpdate(sql);
            return count;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
    }

    /*
    修改投放申请海报信息
    @param applyContentSpScDetailPoster 投放申请海报信息 包含：投放申请海报标识，海报文件，分辨率等
    * @return Integer 修改成功数量
     */
    @Override
    public Integer updateApplyContentSpScDetailPoster(ApplyContentSpScDetailPoster applyContentSpScDetailPoster) {
        try {
            //创建连接
            conn = JdbcUtils.getConnection();
            //根据连接获取statemen对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "UPDATE `bo_adms`.`apply_content_sp_sc_detail_poster` SET " +
                    "`poster_url`='"+applyContentSpScDetailPoster.getPosterUrl()+"'," +
                    "`resolution`= "+applyContentSpScDetailPoster.getResolution()+"," +
                    "`apply_content_sp_sc_detail_id` ="+applyContentSpScDetailPoster.getApplyContentSpScDetailId()+""+
                    " WHERE `id` = "+applyContentSpScDetailPoster.getId()+"";
            int count = stmt.executeUpdate(sql);
            return count;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
    }

    /*
     删除投放申请海报信息
     @param   id 投放申请海报标识
    * @return Integer 删除成功数量
    */
    @Override
    public Integer deleteApplyContentSpScDetailPoster(Long id) {
        try {
            //创建连接
            conn = JdbcUtils.getConnection();
            //根据连接获取statemen对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "DELETE FROM `bo_adms`.`apply_content_sp_sc_detail_poster` WHERE `id` = "+ id;
            int count = stmt.executeUpdate(sql);
            return count;
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
    }

    /*
    查找投放投放申请海报信息
    @param id 投放申请海报标识
    * @return applyContentSpScDetailPoster 投放申请海报信息 包含：投放申请海报标识，海报文件，分辨率等
    */
    @Override
    public ApplyContentSpScDetailPoster getApplyContentSpScDetailPosterById(Long id) {
        ApplyContentSpScDetailPoster applyContentSpScDetailPoster = new ApplyContentSpScDetailPoster();
        try {
            //创建连接
            conn = JdbcUtils.getConnection();
            //根据连接获取statemen对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "SELECT id,poster_url,resolution,apply_content_sp_sc_detail_id FROM `bo_adms`.`apply_content_sp_sc_detail_poster` WHERE `id` = "+id+"";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String postUrl = rs.getString("poster_url");
                long resolution = rs.getLong("resolution");
                long applyContentSpScDetailIdId = rs.getLong("apply_content_sp_sc_detail_id");
                applyContentSpScDetailPoster.setId(id);
                applyContentSpScDetailPoster.setPosterUrl(postUrl);
                applyContentSpScDetailPoster.setResolution(resolution);
                applyContentSpScDetailPoster.setApplyContentSpScDetailId(applyContentSpScDetailIdId);
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
        return applyContentSpScDetailPoster;
    }

    /*
    查询投放申请海报信息
    @param applyContentSpScDetailPoster 投放申请海报信息 包含：投放申请海报标识，海报文件，分辨率等
    * @return List<ApplyContentSpScDetailPoster> 投放申请海报信息集合 包含：投放申请海报标识，海报文件，分辨率等
    */
    @Override
    public List<ApplyContentSpScDetailPoster> searchApplyContentSpScDetailPoster(ApplyContentSpScDetailPoster applyContentSpScDetailPoster) {
        List<ApplyContentSpScDetailPoster> applyContentSpScDetailPosterList = new ArrayList<>();
        try {
            //创建连接
            conn = JdbcUtils.getConnection();
            //根据连接获取statemen对象
            stmt = conn.createStatement();
            //执行sql
            String sql = "SELECT id,poster_url,resolution,apply_content_sp_sc_detail_id FROM `bo_adms`.`apply_content_sp_sc_detail_poster` WHERE 1=1";
            if (null != applyContentSpScDetailPoster.getPosterUrl() && !"".equals(applyContentSpScDetailPoster.getPosterUrl())) {
                sql += "AND poster_url LIKE '%"+applyContentSpScDetailPoster.getPosterUrl()+"%'";
            }
            if (null != applyContentSpScDetailPoster.getResolution()) {
                sql += " AND resolution="+applyContentSpScDetailPoster.getResolution();
            }
            if (applyContentSpScDetailPoster.getApplyContentSpScDetailId() != null) {
                sql += " AND apply_content_sp_sc_detail_id="+applyContentSpScDetailPoster.getApplyContentSpScDetailId();
            }
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                ApplyContentSpScDetailPoster newapplyContentSpScDetailPoster = new ApplyContentSpScDetailPoster();
                long id = rs.getLong("id");
                String posterUrl = rs.getString("poster_url");
                long resolution = rs.getLong("resolution");
                long applyContentSpScDetailId = rs.getLong("apply_content_sp_sc_detail_id");
                newapplyContentSpScDetailPoster.setId(id);
                newapplyContentSpScDetailPoster.setPosterUrl(posterUrl);
                newapplyContentSpScDetailPoster.setResolution(resolution);
                newapplyContentSpScDetailPoster.setApplyContentSpScDetailId(applyContentSpScDetailId);
                applyContentSpScDetailPosterList.add(newapplyContentSpScDetailPoster);
            }
        }catch (SQLException e){
            throw new RuntimeException(e);
        }finally {
            JdbcUtils.close(conn, stmt, rs);
        }
        return applyContentSpScDetailPosterList;
    }
}
